import 'package:flutter_study/project/_01osc/common/event_bus.dart';
import 'package:flutter_study/project/_01osc/constants/constants.dart';
import 'package:flutter_study/project/_01osc/pages/login_web_page.dart';
import 'package:flutter_study/project/_01osc/utils/data_utils.dart';
import 'package:flutter_study/project/_01osc/utils/net_utils.dart';
import 'package:flutter/material.dart';

// 我的
class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  final List menuTitleList = [
    '我的消息',
    '阅读记录',
    '我的博客',
    '我的问答',
    '我的活动',
    '我的团队',
    '邀请好友',
  ];
  final List menuIconList = [
    Icons.message,
    Icons.chrome_reader_mode,
    Icons.camera,
    Icons.live_help_outlined,
    Icons.local_activity_rounded,
    Icons.mobile_friendly,
    Icons.share,
  ];

  @override
  void initState() {
    super.initState();
    _showUserInfo();
    // 注册登录成功消息
    eventBus.on<LoginEvent>().listen((event) {
      _getUserInfo();
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
        itemBuilder: (context, index) {
          if (index == 0) {
            return _buildProfileHeader();
          }
          index -= 1;
          return ListTile(
            leading: Icon(menuIconList[index]),
            title: Text(menuTitleList[index]),
            trailing: Icon(Icons.arrow_forward_ios),
          );
        },
        separatorBuilder: (context, index) => Divider(),
        itemCount: menuTitleList.length + 1);
  }

  // 个人中心头部布局
  Container _buildProfileHeader() {
    return Container(
      color: Color(AppColors.APP_THEME),
      height: 150.0,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
            child: Container(
              height: 60.0,
              width: 60.0,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color: Colors.white,
                  width: 2.0,
                ),
                image: DecorationImage(
                    image: AssetImage('assets/images/ic_avatar_default.png'),
                    fit: BoxFit.cover),
              ),
            ),
            onTap: () {
              _toLoginPage();
            },
          ),
          SizedBox(
            height: 10.0,
          ),
          Text(
            '点击登录',
            style: TextStyle(color: Colors.white),
          )
        ],
      ),
    );
  }

  // 跳转登录页面
  _toLoginPage() async {
    final result = await Navigator.push(
        context, MaterialPageRoute(builder: (context) => LoginWebPage()));
    if (result != null && result == 'refresh') {
      // 表示登录成功
      eventBus.fire(LoginEvent());
    }
  }

  // 从缓存获取用户信息
  void _showUserInfo() {}

  // 请求用户信息
  void _getUserInfo() {
    // 判断是否有token
    DataUtils.getToken().then((value) => null);
    Map<String, dynamic> params = Map();
    NetUtils.get(AppUrls.OPENAPI_USER, params);
  }
}
